<script>

import {delDepartment, deleteBatch, edit, list, save} from "@/api/department";
import {findAllDoctorNames} from "@/api/doctor";

export default {
  name: "Department",
  data() {
    return {
      tableData: [],
      name: "",
      total: 0,
      loading: true,
      multipleSelection: [],
      dialogFormVisible: false,
      form: {},
      doctorNameList:[],
      formLabelWidth: '150px',
      dialogFormVisible1: false,
      dialogFormVisible2:false
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      let params={
        name: this.name
      }
      list(params).then(res => {
        console.log(res.data)
        this.tableData = res.data;
        this.loading = false;
        this.total = res.data.total;
      });
    },
    reset() {
      this.name = "";
      this.currentPage = 1;
      this.pageSize = 5;
      this.total = 0;
      this.load();
    },
    handleDelete(id) {
      delDepartment(id).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            showClose: true,
            message:  res.msg,
            type: 'error'
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((a) => a.id);
      console.log(this.multipleSelection);
    },
    handleBatchRemove() {
      let ids = this.multipleSelection;
      deleteBatch(ids).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '删除成功',
            type: 'success',
            center: true
          });
          this.load();
        } else {
          this.$message({
            center: true,
            message:  res.msg,
            type: 'error'
          });
        }
      })
    },
    handleEdit(row) {
      this.form = row;
      this.findAllDoctorNames();
      this.dialogFormVisible1 = true;
    },
    edit() {
      edit(this.form).then(res=>{
        if (res.code === 200) {
          this.$message({
            message: '科室修改成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible1 = false;
          this.form = {};
          this.doctorNameList=[];
          this.load();
        } else {
          this.$message({
            message: res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    findAllDoctorNames(){
      findAllDoctorNames().then(res=>{
        if (res.code === 200) {
          this.doctorNameList=res.data;
        }else{
          this.$message({
            message: "医生列表获取失败",
            type: 'error',
            center: true
          });
        }
      })
    },
    handleAddSecondClassDepartment(parentId) {
      this.form = {};
      this.form.parentId = parentId;
      this.findAllDoctorNames();
      this.dialogFormVisible2 = true;
    },
    handleSave(){
      this.form={};
      this.findAllDoctorNames();
      this.dialogFormVisible=true;
    },
    save() {
      save(this.form).then(res => {
        if (res.code === 200) {
          this.$message({
            message: '科室创建成功',
            type: 'success',
            center: true
          });
          this.dialogFormVisible = false;
          this.dialogFormVisible2 = false;
          this.form = {};
          this.doctorNameList=[];
          this.load();
        } else {
          this.$message({
            message:  res.msg,
            type: 'error',
            center: true
          });
        }
      })
    },
    headerBg({row,rowIndex}){
      if(rowIndex==0){
        return 'headerBgClass';
      }
    }
  }
}
</script>

<template>
  <div>
    <div class="search">
      <el-input style="width:160px" v-model="name" suffix-icon="el-icon-search" placeholder="请输入科室名称"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="load">搜索</el-button>
      <el-button type="warning" style="margin-left: 5px" @click="reset">重置</el-button>
    </div>
    <div style="margin:10px 0px" class="operation">
      <el-button type="primary" class="ml-5" icon="el-icon-circle-plus-outline" @click="handleSave">新增
      </el-button>
      <el-dialog title="新增科室" :visible.sync="dialogFormVisible" width="30%" center>
        <el-form :model="form" label-width="100px" style="width:340px">
          <el-form-item label="科室名称" >
            <el-input v-model="form.name" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="科室描述">
            <el-input type="textarea" v-model="form.description" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="科室主管" >
           <el-select v-model="form.leader" filterable placeholder="请选择">
             <el-option v-for="item in doctorNameList" :key="item.doctorName"
                        :label="item.doctorName+'-'+item.departmentName" :value="item.doctorName">
             </el-option>
           </el-select>
          </el-form-item>
          <el-form-item label="联系方式" >
            <el-input v-model="form.phone" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="邮箱地址" >
            <el-input v-model="form.email" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="科室位置" >
            <el-input v-model="form.address" autocomplete="off" clearable></el-input>
          </el-form-item>
          <el-form-item label="显示排序">
            <el-input-number v-model="form.sortNum" controls-position="right" :min="1" :max="99"></el-input-number>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="save">确 定</el-button>
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除吗？"
          @confirm="handleBatchRemove"
          class="ml-5"
      >
        <el-button type="danger" class="ml-5" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
    </div>
    <div class="table">
      <el-table :data="tableData"
                border stripe
                v-loading="loading"
                max-height="550"
                row-key="id"
                :tree-props="{children:'secondClassDepartmentList',hasChildren:'hasChildren'}"
                :header-cell-class-name="headerBg"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(255,225,255, 0.1)"
                @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55px" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="58px" align="center">
        </el-table-column>
        <el-table-column prop="name" label="科室名称" width="100px" align="center">
        </el-table-column>
        <el-table-column prop="description" label="科室描述" :show-overflow-tooltip=true align="center">
        </el-table-column>
        <el-table-column prop="leader" label="科室主管" align="center">

        </el-table-column>
        <el-table-column prop="phone" label="联系方式" width="120px" align="center">
        </el-table-column>
        <el-table-column prop="email" label="邮箱地址"  :show-overflow-tooltip="true" align="center">
        </el-table-column>
        <el-table-column prop="address" label="科室位置" align="center">
        </el-table-column>
        <el-table-column prop="sortNum" label="排序" width="70" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="310px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-plus" class="mr-5" v-if="!scope.row.parentId" @click="handleAddSecondClassDepartment(scope.row.id)">新增二类科室</el-button>
            <el-dialog title="新增二类科室" :visible.sync="dialogFormVisible2" width="30%" :modal-append-to-body="false" center>
              <el-form :model="form" label-width="100px" style="width:340px">
                <el-form-item label="科室名称" >
                  <el-input v-model="form.name" autocomplete="off" ></el-input>
                </el-form-item>
                <el-form-item label="科室描述">
                  <el-input type="textarea" v-model="form.description" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="科室主管" >
                  <el-select v-model="form.leader" filterable placeholder="请选择">
                    <el-option v-for="item in doctorNameList" :key="item.doctorName"
                               :label="item.doctorName+'-'+item.departmentName" :value="item.doctorName">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="联系方式" >
                  <el-input v-model="form.phone" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="邮箱地址" >
                  <el-input v-model="form.email" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="科室位置" >
                  <el-input v-model="form.address" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="显示排序">
                   <el-input-number v-model="form.sortNum" controls-position="right" :min="1" :max="99"></el-input-number>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="save">确 定</el-button>
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
              </div>
            </el-dialog>
            <el-button type="success" icon="el-icon-edit" style="margin-left: 5px" @click="handleEdit(scope.row)">编辑</el-button>
            <el-dialog title="编辑科室" :visible.sync="dialogFormVisible1" width="30%"  :modal-append-to-body="false" center>
              <el-form :model="form" label-width="100px" style="width:340px">
                <el-form-item label="科室名称" >
                  <el-input v-model="form.name" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="科室描述">
                  <el-input type="textarea" v-model="form.description" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="科室主管" >
                  <el-select v-model="form.leader" filterable placeholder="请选择">
                    <el-option v-for="item in doctorNameList" :key="item.doctorName"  :label="item.doctorName+'-'+item.departmentName" :value="item.doctorName">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="联系方式" >
                  <el-input v-model="form.phone" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="邮箱地址" >
                  <el-input v-model="form.email" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="科室位置" >
                  <el-input v-model="form.address" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="显示排序">
                  <el-input-number v-model="form.sortNum" controls-position="right" :min="1" :max="99"></el-input-number>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="edit">确 定</el-button>
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
              </div>
            </el-dialog>
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定删除吗？"
                @confirm="handleDelete(scope.row.id)"
            >
              <el-button type="danger" icon="el-icon-delete" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<style scoped>
.headerBgClass{
  background-color: #cccccc !important;
}
</style>
